<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Student_model extends CI_Model{
	
	public function __construct()
	{
	parent::__construct();
	}
	
	public function check_username($uname)
	{
		$this->load->database();
		
		$sql = "SELECT username FROM student_account WHERE username = ?";
		$query = $this->db->query($sql, $uname);

		if ($query->num_rows() > 0)
			return true;
		return false;
	}
	
	
	public function check_login($account)
	{
		$this->load->database();
		
		$sql = "SELECT username, password FROM student_account WHERE username = ? AND password = ?"; 
		$query = $this->db->query($sql, array($account['username'], $account['password']));
	
		if($query->num_rows() == 0)
			$query = NULL;
	
		return $query;
	}
	
	public function getStudData($username) {
	
		$this->load->database();
		
		/* Getting values from student_account table. */
		$this->db->select('name, student_no, provincial_address, college_address, mobile_number, birthday,email');
		$this->db->where('username',$username);
		$query = $this->db->get('student_account');
	
		foreach($query->result() as $row)
			$d['name'] = $row->name;
			$d['stdno'] = $row->student_no;
			$d['provincial_address'] = $row->provincial_address;
			$d['college_address'] = $row->college_address;
			$d['mobile_number'] = $row->mobile_number;
			$d['birthday'] = $row->birthday;
			$d['email'] = $row->email;
		return $d;
	}
	
	public function getStudData2($stdno) {
		$this->load->database();
		
		/* Getting values from student_account table. */
		$this->db->select('name,student_no,provincial_address, college_address, mobile_number, birthday,email,password');
		$this->db->where('student_no',$stdno);
		$query = $this->db->get('student_account');
	
		foreach($query->result() as $row)
			$d['name'] = $row->name;
			$d['student_no'] = $row->student_no;
			$d['provincial_address'] = $row->provincial_address;
			$d['college_address'] = $row->college_address;
			$d['mobile_number'] = $row->mobile_number;
			$d['birthday'] = $row->birthday;
			$d['email'] = $row->email;
			$d['password'] = $row->password;
		return $d;
	}
	
	public function addToLog($message,$name){
		$this->load->database();
		$d = array(
			'name' => $name,
			'action' => $message,
		);	
		$this->db->insert('log', $d); 
	}
	
	public function modifybasicinfo($data){
		$this->load->database();
		
		$stdno = $data['stdno'];
		$data = array(
			'provincial_address' => $data['p_address'],
			'college_address' => $data['c_address'],
			'mobile_number' => $data['cpno'],
			'birthday' => $data['bday'],
			'email' => $data['email'],
		);
		echo $data['cpno'];
		$this->db->where('student_no', $stdno);
		$this->db->update('student_account', $data); 
	}
	
	public function changePW($data){
		$this->load->database();
		$stdno = $data['stdno'];
		$data = array(
			'password' => $data['pw'],
			);
		$this->db->where('student_no', $stdno);
		$this->db->update('student_account', $data); 
	}

	public function check_stdno($stdno)
	{
		$this->load->database();
		$sql = "SELECT student_no FROM student_account WHERE student_no = ?";
		$query = $this->db->query($sql, array($stdno));
		if ($query->num_rows() > 0)
			return true; /*Student Number exists.*/
		return false;
	}
	
	public function check_activeaccount($stdno)
	{
		$this->load->database();
		$sql = "SELECT activated FROM student_account WHERE student_no = ?";
		$query = $this->db->query($sql, array($stdno));
		$activated=0;
		foreach ($query->result() as $row)
			$activated = $row->activated;
			
		if($activated == 1)
			return true; /*Student Account is activated.*/
		return false;
	}

	public function add_account($account)
	{
		$this->load->database();
		$sql = "INSERT INTO student_account(student_no,name,approved,activated,username,password,email) VALUES (?,?,?,?,?,?,?)";
		$query = $this->db->query($sql, array($account['stdno'], $account['name'],
			$account['approved'], $account['activated'], $account['username'], $account['password'], $account['email']));	
		return $query;
	}

	public function delete_account($username) {
		$this->load->database();
		$this->db->where('username', $username);
		$this->db->delete('student_account');
	}
	
	public function getUnapproved() {
		$this->load->database();
		$sql = "SELECT username FROM student_account WHERE approved = 0";
		$query = $this->db->query($sql);	
		return $query->result_array();
	}

	public function approve($username) {
		$this->load->database();
		$this->db->where('username', $username);
		$this->db->update('student_account', array('approved' => 1));
	}
	
	public function studAcadInfo($stdno){
		$this->load->database();
		
		/* Getting values from student_account table. */
		$sql = "SELECT student_no,reg_adviser,sp_adviser,curriculum FROM student_account WHERE student_no = ?"; 
		$query = $this->db->query($sql, $stdno);
		
		foreach($query->result() as $row)
			$d['stdno'] = $row->student_no;
			$d['reg_adviser'] = $row->reg_adviser;
			$d['sp_adviser'] = $row->sp_adviser;
			$d['curriculum'] = $row->curriculum;
			
		/* Get names of SP Adviser and Reg Adviser */
		$this->db->select('name');
		$this->db->where('emp_id', $d['reg_adviser']);
		$query = $this->db->get('instructor_account');
		
		foreach($query->result() as $row){
			$d['reg_adviser'] = $row->name;
		}
		
		$this->db->select('name');
		$this->db->where('emp_id', $d['sp_adviser']);
		$query = $this->db->get('instructor_account');
		
		foreach($query->result() as $row){
			$d['sp_adviser'] = $row->name;
		}
		
		/* Getting the list of approved GEs. */
		$sql = "SELECT course_name FROM approved_ge WHERE student_no_fk = ?";
		$query = $this->db->query($sql, $stdno);
		$approved_ge = array();
		foreach($query->result() as $row){
			$approved_ge[] = $row->course_name;
		}
		$d['approved_ge'] = $approved_ge;
		
		/* Getting the list of approved Electives. */
		$sql = "SELECT course_name FROM approved_electives WHERE student_no_fk = ?";
		$query = $this->db->query($sql, $stdno);
		$approved_electives = array();
		foreach($query->result() as $row){
			$approved_electives[] = $row->course_name;
		}
		$d['approved_electives'] = $approved_electives;
		
		/* Getting the list of subjects taken. */
		$sql = "SELECT course_no_fk, units, grade, sem_taken, year, taken_type FROM subjects_taken WHERE student_no_fk = ?";
		$query = $this->db->query($sql, $stdno);
		$subjects_taken = array();
	
		foreach($query->result() as $row){
			$subjects_taken[] = array(
							'course_no_fk' => $row->course_no_fk,
							'units' => $row->units,
							'grade' => $row->grade,
							'sem' => $row->sem_taken,
							'year' => $row->year,
							'type' => $row->taken_type,
				);
		}
		$d['subjects_taken'] = $subjects_taken;
		
		/* Cases Involved */
		$sql = "SELECT case_description,date1 FROM cases WHERE student_no_fk = ?";
		$query = $this->db->query($sql, $stdno);
		$cases = array();
		foreach($query->result() as $row){
			$cases[] = array(
					'case_description' => $row->case_description,
					'date' => $row->date1,
				);
		}
		$d['cases'] = $cases;
		
		/* Academic History */
		$sql = "SELECT semester,year,gwa,standing FROM academic_history WHERE student_no_fk = ?";
		$query = $this->db->query($sql, $stdno);
		$academic_history = array();
	
		foreach($query->result() as $row){
			$academic_history[] = array(
							'sem' => $row->semester,
							'year' => $row->year,
							'gwa' => $row->gwa,
							'standing' => $row->standing,
				);
		}
		$d['academic_history'] = $academic_history;
		
		/* Adviser History */
		$sql = "SELECT emp_no FROM prev_sp_adviser WHERE stud_no_fk = ?";
		$query = $this->db->query($sql, $stdno);
		$spadviser_history = array();
		
		foreach($query->result() as $row){
			/* Retrieve name of adviser */
			$this->db->select('name');
			$this->db->where('emp_id', $row->emp_no);
			$query = $this->db->get('instructor_account');
			
			foreach($query->result() as $row2){
				$spadviser_history[] = array(
					'emp_name' => $row2->name,
				);
			}
		}
		$d['spadviser_history'] = $spadviser_history;
		
		return $d;
	}
	
	public function getName($stdno){
		$this->load->database();
		
		$this->db->select('name');
		$this->db->where('student_no',$stdno);
		$query = $this->db->get('student_account');
	
		foreach($query->result() as $row)
			$name = $row->name;
		return $name;
	}

	public function activate($username) {
		$this->load->database();
		$this->db->where('username', $username);
		$this->db->update('student_account', array('activated' => 1));
	}

	public function deactivate($username) {
		$this->load->database();
		$this->db->where('username', $username);
		$this->db->update('student_account', array('activated' => 0));
	}

	public function getActivatedStatus() {
		$this->load->database();
		$this->db->select('username, activated');
		$this->db->where('approved', 1);
		$query = $this->db->get('student_account');

		return $query->result_array();
	}
	
	public function getAllUsernames() {
		$this->load->database();
		$this->db->select('username');
		$this->db->where('approved', 1);
		$query = $this->db->get('student_account');

		return $query->result_array();
	}

	public function sendMessage($username, $detail) {
		$this->load->database();
		$this->db->insert('message', array(
				'sender' => $username,
				'message' => $detail,
				'receiver' => 'admin'
			));
	}

	public function getMessages($username) {
		$this->load->database();
		$this->db->where('receiver', $username);
		$query = $this->db->get('message');

		return $query->result_array();
	}
}


/* End of file student_model.php */
/* Location: ./application/models/student_model.php */
